package com.sky.module.system.post.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.module.support.log.annotation.SysOperLog;
import com.sky.module.support.log.enums.BusinessType;
import com.sky.module.system.post.domain.entity.Post;
import com.sky.module.system.post.domain.vo.PostQueryVo;
import com.sky.module.system.post.service.PostService;
import com.sky.utils.Result;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author
 * @date 2023/2/27 23:22
 **/
@RestController
@RequestMapping("/api/post")
public class PostController {

    @Autowired
    private PostService postService;

    @GetMapping("list")
    public Result list(PostQueryVo postQueryVo) {
        Page<Post> page = postService.list(postQueryVo);
        return Result.ok(page);
    }

    /***
     * @description SysOperLog进行日志收集
     * @param post
     * @return com.sky.utils.Result
     * @date 2023/3/8 8:42
     *
     */
    @SysOperLog(title = "添加岗位模块", businessType = BusinessType.INSERT, value = "进行岗位的添加")
    @PostMapping("add")
    public Result add(@RequestBody Post post) {
        if (postService.save(post)) {
            return Result.ok().message("添加岗位成功");
        }
        return Result.error();
    }

    @SysOperLog(title = "删除岗位模块", businessType = BusinessType.DELETE, value = "删除岗位模块")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        if (postService.removeById(id)) {
            return Result.ok();
        }
        return Result.error();
    }

    @SysOperLog(title = "修改岗位模块", businessType = BusinessType.UPDATE, value = "修改岗位模块")
    @PutMapping("update")
    public Result update(@RequestBody Post post) {
        if (postService.update(post)) {
            return Result.ok().message("修改岗位成功");
        }

        return Result.error();
    }

    @GetMapping("test")
    public Result test() {
        return Result.error();
    }
}
